<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
    <#list  columns as field>
      <el-form-item label="${field.columnComment!}" prop="${field.columnName!}">
        <el-input
            v-model="queryParams.${field.columnName!}"
            placeholder="请输入${field.columnComment!}"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </#list>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="end">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
    <#list  columns as field>
      <el-table-column label="${field.columnComment!}" align="center" prop="${field.columnName!}"/>
    </#list>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        v-show="this.total>0"
        :current-page.sync="queryParams.pageNumber"
        :page-size.sync="queryParams.pageSize"
        :total="this.total"
        layout='total, sizes, prev, pager, next, jumper'
        :page-sizes="[10, 20, 30, 50]"
        @size-change="sizeChange"
        @current-change="pageChange"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <#list  columns as field>
        <el-form-item label="${field.columnComment!}" prop="${field.columnName!}">
          <el-input v-model="form.${field.columnName!}" placeholder="请输入${field.columnComment!}"/>
        </el-form-item>
      </#list>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "${tableName}Index",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    pageChange(page){
      this.queryParams.pageNumber=page
      this.getList()
    },
    sizeChange(size){
      this.queryParams.pageSize=size
      this.getList()
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.post('/neu/${bigModuleName}/${tableName}/page', this.queryParams).then(response => {
        this.dataList = response.list;
        this.total = response.totalRow;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNumber = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      this.post('/neu/${bigModuleName}/${tableName}/getById', {id}).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.post('/neu/${bigModuleName}/${tableName}/upsert', this.form).then(response => {
            this.$message({
              message: '保存成功！',
              type: 'success'
            });
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = [];
      if (row) {
        ids.push(row.id)
      } else {
        ids = this.ids;
      }
      this.$confirm("是否确认删除", '确认', {
        confirmButtonText: '确定删除',
        showCancelButton: true,
      }).then((res) => {
        this.post('/neu/${bigModuleName}/${tableName}/remove', {ids}).then(response => {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.open = false;
          this.getList();
        });
      }).catch(() => {
      });
    },
  }
};
</script>
